package com.dps.omc.email;

import com.dps.omc.enumer.EnumJobAuditState;
import com.dps.omc.enumer.EnumJobState;
import com.dps.omc.model.*;
import com.dps.omc.service.*;
import com.dps.omc.task.message.EnumTaskState;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by lip on 2016-04-03.
 */
@Service
public class JobEmailService {

    @Autowired
    private EmailUtil emailUtil;
    @Autowired
    private IUserService userService;
    @Autowired
    private ITaskService taskService;
    @Autowired
    private IJobBizTypeService jobBizTypeService;
    @Autowired
    private IJobBizUnitService jobBizUnitService;
    @Autowired
    private IUserBizRightsService userBizRightsService;

    public void sendByJobStatus(Job job)
    {
        List<UserBizRights> userBizRightsList = this.getUserBizRightsList(job);
        String subject = this.getSubjectByStatus(job);
        String msg = this.getMsgByStatus(job);
        for(UserBizRights userBizRights:userBizRightsList)
        {
            emailUtil.send(userBizRights.getEmail(),subject,msg);
        }

    }

    public void sendByAuditState(Job job)
    {
        List<UserBizRights> userBizRightsList = this.getUserBizRightsList(job);
        String subject = this.getSubjectByAuditState(job);
        String msg = this.getMsgByAuditState(job);
        for(UserBizRights userBizRights:userBizRightsList)
        {
            emailUtil.send(userBizRights.getEmail(),subject,msg);
        }
    }


    private List<UserBizRights> getUserBizRightsList(Job job)
    {
        List<JobBizUnit> jobBizUnits = jobBizUnitService.findList("jobId",job.getId());
        List<JobBizType> jobBizTypes = jobBizTypeService.findList("jobId",job.getId());
        List<String> bizUnits = new ArrayList<>();
        List<String> bizTypes = new ArrayList<>();
        for(JobBizUnit jobBizUnit:jobBizUnits)
        {
            bizUnits.add(jobBizUnit.getBizUnit());
        }
        for(JobBizType jobBizType:jobBizTypes)
        {
            bizTypes.add(jobBizType.getBizType());
        }
        List<UserBizRights> userBizRightsList = userBizRightsService.findUsersByUnitsAndTypes(bizUnits,bizTypes);
        return userBizRightsList;
    }

    private String getSubjectByStatus(Job job)
    {
        if(job.getStatus().equals(EnumJobState.FAIL))
        {
            return "作业"+job.getName()+"发布异常";
        }
        return "作业"+job.getName()+job.getStatus().getLabel();
    }

    private String getMsgByStatus(Job job)
    {
        if(job.getStatus().equals(EnumJobState.FAIL))
        {
            Map<String,Object> condition = new HashMap<>();
            condition.put("jobId",job.getId());
            condition.put("status", EnumTaskState.FAIL);
            List<Task> tasks = taskService.queryList(condition,"seq","asc");
            StringBuffer sb = new StringBuffer();
            sb.append("共"+tasks.size()+"条任务执行异常");
            sb.append("\n\n\n");
            for(Task task:tasks)
            {
                sb.append("任务"+task.getNote()+"异常日志\n");
                sb.append(task.getReturnMsg());
                sb.append("\n\n\n");
            }
            return sb.toString();
        }
        return "作业"+job.getName()+job.getStatus().getLabel();
    }

    private String getSubjectByAuditState(Job job)
    {
        if(job.getAuditState().equals(EnumJobAuditState.AUDIT_YES))
        {
            return "作业"+job.getName()+"审核通过";
        }
        if(job.getAuditState().equals(EnumJobAuditState.AUDIT_NO))
        {
            return "作业"+job.getName()+"审核驳回";
        }
        return "作业"+job.getName()+job.getAuditState().getLabel();
    }

    private String getMsgByAuditState(Job job)
    {
        if(job.getAuditState().equals(EnumJobAuditState.AUDIT_YES))
        {
            return "作业"+job.getName()+"审核通过";
        }
        if(job.getAuditState().equals(EnumJobAuditState.AUDIT_NO))
        {
            return "作业"+job.getName()+"审核驳回";
        }
        return "作业"+job.getName()+job.getAuditState().getLabel();
    }

    public void sendJobMsg(Job job)
    {
        String userId = job.getCreaterId();
        User user = userService.fetch(userId);
        emailUtil.send(user.getEmail(),this.getSubject(job),this.getMsg(job));
    }

    private String getSubject(Job job)
    {
        return "作业"+job.getName()+"发布异常";
    }

    private String getMsg(Job job)
    {
        Map<String,Object> condition = new HashMap<>();
        condition.put("jobId",job.getId());
        condition.put("status", EnumTaskState.FAIL);
        List<Task> tasks = taskService.queryList(condition,"seq","asc");
        StringBuffer sb = new StringBuffer();
        sb.append("共"+tasks.size()+"条任务执行异常");
        sb.append("\n\n\n");
        for(Task task:tasks)
        {
            sb.append("任务"+task.getNote()+"异常日志\n");
            sb.append(task.getReturnMsg());
            sb.append("\n\n\n");
        }
        return sb.toString();
    }
}
